package com.superman.supermarket.client.activity;

import java.util.HashMap;
import java.util.Map;

import com.google.gwt.event.shared.EventBus;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.user.client.Window;
import com.google.inject.Inject;
import com.superman.mvpframe.client.ajax.PostManAsyncCallBack;
import com.superman.mvpframe.client.event.ToastEvent;
import com.superman.mvpframe.client.mvp.IPageView;
import com.superman.mvpframe.client.mvp.IPageViewPresenter;
import com.superman.mvpframe.client.utils.Registry;
import com.superman.mvpframe.client.utils.StringUtils;
import com.superman.mvpframe.client.utils.XJsonUtil;
import com.superman.supermarket.client.event.PreorderPageEvent;
import com.superman.supermarket.client.mvp.ShopAbstractActivity;
import com.superman.supermarket.client.utils.ActivityJumpUrl;
import com.superman.supermarket.client.view.preorder.PreorderView;
import com.superman.supermarket.client.view.preorder.PreorderView.PreorderViewPresenter;
import com.superman.supermarket.client.widget.payment.PaymentType;

/**
 * 确认订单
 * 
 * @author superman
 * @version 2018年8月2日下午5:30:35
 */
public class PreorderActivity extends ShopAbstractActivity implements PreorderViewPresenter {

	private String addrId, couponId;

	private Map<String, Object> addrMap;

	private int cleanCart = 0;

	private Map<String, Object> preorderInfoMap;

	@Inject
	PreorderView preorderView;

	private Map<String, Object> settlementMap;

	@Override
	public void addOrder() {
		if (StringUtils.isEmpty(this.addrId)) {
			eventBus.fireEvent(new ToastEvent("请先填写收货地址"));
		} else {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("ADDR_ID", this.addrId);
			if (!StringUtils.isEmpty(couponId))
				map.put("COUPON", this.couponId);
			map.put("STORE_SKU_LIST", preorderView.getStoreSkuList());
			map.put("PAYMENT", PaymentType.ALIPAY.getType());
			map.put("CARRY_MODE", preorderView.getCarryMode());
			map.put("TOTAL_PRICE", preorderView.getTotalFee());
			map.put("CLEAN_CART", this.cleanCart);
			map.put("RETURN_URL", Window.Location.getProtocol() + Window.Location.getHost() + Window.Location.getPath());

			postman.post("order/add2", map, new PostManAsyncCallBack() {

				@Override
				public void onBusinessError(String statusCode) {

				}

				@Override
				public void onBusinessOK(String response) {
					Map<String, Object> map = XJsonUtil.getBody(response);
					String orderid = String.valueOf(map.get("ORDER_ID"));
					ActivityJumpUrl.pushToCasherDeskPage(orderid);
				}
			});
		}
	}

	@Override
	public IPageView getView() {
		return preorderView;
	}

	@Override
	public IPageViewPresenter getViewPresenter() {
		return this;
	}

	@Override
	public void onActivity(EventBus eventBus) {
		eventBus.addHandler(PreorderPageEvent.TYPE, new PreorderPageEvent.PreorderPageEventHandler() {

			@Override
			public void pushToSkuDetailPage(Map<String, Object> map) {
				// TODO Auto-generated method stub

			}

			@Override
			public void pushToStorePage(String storeid) {
				// TODO Auto-generated method stub

			}

			@Override
			public void resetCouponId(String obj) {
				couponId = obj;
			}

			@Override
			public void settlementByCoupon(String couponid) {
				couponId = couponid;
				settlementByCouponId();
			}
		});
	}

	private void parseBySettlementMap() {
		if (settlementMap.get("ADDR") == null) {
			this.addrId = "";
			preorderView.setNoAddrInfo();
		} else {
			this.addrMap = XJsonUtil.getMap((JSONObject) settlementMap.get("ADDR"));
			this.addrId = String.valueOf(this.addrMap.get("ADDR_ID"));
			preorderView.setAddrInfo(addrMap);
		}
		preorderView.setStoreList(XJsonUtil.getList((JSONArray) settlementMap.get("STORE_SKU_LIST")));
		preorderView.setOrderInfo(settlementMap);

	}

	@Override
	public void popToRefresh() {
		// TODO Auto-generated method stub

	}

	@Override
	public void popToRefresh(Object contextData) {
		// 返回刷新,重新走结算的协议,此流程只有选择地址和选择优惠券后会处理
		Map<String, Object> map = (Map<String, Object>) contextData;
		if (map != null) {
			if (map.get("ADDR_ID") != null) {
				String addrId = String.valueOf(map.get("ADDR_ID"));
				settlementByAddrId(addrId);
			}
		}

	}

	@Override
	public void pushToChooseAddr() {
		if (addrMap == null) {
			// 跳转到新建地址的页面
			placeController.push(placeFactory.getAddressNewPlace());
		} else {
			// 跳转到选择地址的页面
			String addrId = String.valueOf(addrMap.get("ADDR_ID"));
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("ADDR_ID", addrId);
			placeController.push(placeFactory.getAddressChoosePlace(), map);
		}
	}

	@Override
	public void pushToRefresh() {
		if (Window.Location.getParameter("CLEAN_CART") != null)
			this.cleanCart = Window.Location.getParameter("CLEAN_CART").equals("1") ? 1 : 0;
		if (Registry.get("settlementinfo") != null) {
			settlementMap = Registry.get("settlementinfo");
			preorderInfoMap = Registry.get("preorderinfo");

			parseBySettlementMap();

			// 移除
			Registry.unregister("settlementinfo");
			Registry.unregister("preorderinfo");
		}
	}

	private void settlementByAddrId(String addrId) {
		preorderInfoMap.put("ADDR_ID", addrId);
		postman.post("order/settlement2", preorderInfoMap, new PostManAsyncCallBack() {

			@Override
			public void onBusinessError(String statusCode) {

			}

			@Override
			public void onBusinessOK(String response) {
				PreorderActivity.this.settlementMap = XJsonUtil.getBody(response);
				parseBySettlementMap();
			}
		});
	}

	private void settlementByCouponId() {
		preorderInfoMap.put("COUPON", couponId);
		postman.post("order/settlement2", preorderInfoMap, new PostManAsyncCallBack() {

			@Override
			public void onBusinessError(String statusCode) {

			}

			@Override
			public void onBusinessOK(String response) {
				PreorderActivity.this.settlementMap = XJsonUtil.getBody(response);
				parseBySettlementMap();
			}
		});
	}

}
